package com.wanxi.service.impl;

import com.alibaba.fastjson.JSON;
import com.wanxi.feign.PatientClient;
import com.wanxi.feign.DoctorFeignClient;
import com.wanxi.feign.ProductFeignClient;
import com.wanxi.model.PatientQuery;
import com.wanxi.model.ProductInstanceQuery;
import com.wanxi.model.ProductQuery;
import com.wanxi.result.CommonResult;
import com.wanxi.service.FrontDoctorService;
import com.wanxi.vo.DoctorDetail;
import com.wanxi.vo.DoctorQuery;
import com.wanxi.vo.DoctorDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
@Service
public class FrontDoctorServiceImpl implements FrontDoctorService {

    @Autowired
    private DoctorFeignClient doctorFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;


    @Autowired
    private PatientClient patientClient;

    @Override
    public CommonResult findDoctor(DoctorQuery doctorQuery) {
        CommonResult commonResult = doctorFeignClient.findAllFront(doctorQuery);
        if (commonResult.getCode() == 200){
            return commonResult;
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult findProductByDoctorId(Long doctorId) {
        ProductQuery productQuery  = new ProductQuery();
        productQuery.setDoctorId(doctorId);
        CommonResult commonResult = productFeignClient.findProduct(productQuery);
        if (commonResult.getCode() == 200){
            return commonResult;
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult findProductEntityByDoctorId(Long doctorId, Integer serviceStatus) {
        ProductInstanceQuery productInstanceQuery = new ProductInstanceQuery();
        productInstanceQuery.setDoctorId(doctorId);
        productInstanceQuery.setServiceStatus(serviceStatus);
        CommonResult commonResult = productFeignClient.findProductInstanceBack(productInstanceQuery);
        if (commonResult.getCode() == 200){
            return commonResult;
        }else {
            return CommonResult.failed();
        }
    }

    @Override
    public CommonResult findPatientByDoctorId(Long doctorId) {
        PatientQuery patientQuery = new PatientQuery();
        patientQuery.setDoctorId(doctorId);
        CommonResult commonResult = patientClient.findPatient(patientQuery);
        if (commonResult.getCode() == 200){
            return commonResult;
        }else {
            return CommonResult.failed();
        }
    }


    @Override
    public CommonResult findDoctorDetail(Long doctorId) {
        DoctorQuery doctorQuery  = new DoctorQuery();
        doctorQuery.setDoctorId(doctorId);
        //查询医生信息
        CommonResult allFront = doctorFeignClient.findAllFront(doctorQuery);
        if (allFront.getCode() == 200){
            List<LinkedHashMap> doctors = (List<LinkedHashMap>) allFront.getData();
            LinkedHashMap doctorVOHashMap = doctors.get(0);
            DoctorDTO doctorDTO = JSON.parseObject(JSON.toJSONString(doctorVOHashMap), DoctorDTO.class);
            ProductQuery productQuery = new ProductQuery();
            productQuery.setDoctorId(doctorId);
            //查询医生支撑的服务包信息
            CommonResult productCommonResult = productFeignClient.findProduct(productQuery);
            if (productCommonResult.getCode() == 200){
                List<LinkedHashMap> productVos = (List<LinkedHashMap>) productCommonResult.getData();
                DoctorDetail doctorDetail = new DoctorDetail();
                doctorDetail.setDoctorDTO(doctorDTO);
                doctorDetail.setProductVo(productVos);
                return CommonResult.success(doctorDetail);
            }else {
                return CommonResult.failed();
            }
        }else {
            return CommonResult.failed();
        }
    }
}
